package com.example.zhiran.controller;

import com.example.zhiran.exception.OptionResultInitialException;
import com.example.zhiran.pojo.front.SignupUser;
import com.example.zhiran.pojo.option.OptionResult;
import com.example.zhiran.pojo.option.useroption.UserOption;
import com.example.zhiran.pojo.option.useroption.UserOptionMessage;
import com.example.zhiran.pojo.orm.ZRUser;
import com.example.zhiran.service.OptionResultBuilder;
import com.example.zhiran.service.userservice.ZRUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@CrossOrigin("localhost:8081/")
@RestController
@RequestMapping("user")
public class ZRUserController {

    @Autowired
    private ZRUserService zrUserService;

    //登录
    @GetMapping(value = "/login")
    public OptionResult userLogin(@RequestParam(name = "account")String account, @RequestParam(name = "password") String password){
        System.out.println("用户登录");
        return zrUserService.userLogin(account,password);
    }



    //查看userinfo
    @GetMapping(value = "/info",params = {"zrId"})
    public ZRUser getUserById(@RequestParam(name = "zrId")String zrId){
        System.out.println(zrId);
        return zrUserService.getUserById(zrId);
    }

    //查看userinfo
    @GetMapping(value = "/info",params = {"zrNickname"})
    public ZRUser getUserByName(@RequestParam(name = "zrNickname")String name){
        System.out.println(name);
        return zrUserService.getUserByName(name);
    }



    //找回密码
    @GetMapping(value = "/retrieve",params = {"zrTelephone"})
    public ZRUser  getPasswordByTel(@RequestParam(name = "zrTelephone")String tel){
        System.out.println("tel");
        return zrUserService.getUserByTel(tel);
    }

    //增加
    @GetMapping("/signup")
    @ResponseBody
    public OptionResult userSignup(SignupUser signupUser) throws OptionResultInitialException {
        System.out.println(signupUser);
        return zrUserService.addUser(signupUser);
    }

    //昵称是否被占用
    @GetMapping(value = "/signup",params = {"zrNickname"})
    public OptionResult isNicknameOccupied(@RequestParam(name = "zrNickname")String name){
        OptionResultBuilder optionResultBuilder = new OptionResultBuilder()
                .setOptionCode(UserOption.USER_SIGNUP.getOptionCode());
        if (zrUserService.isNickNameOccupied(name)){
            return optionResultBuilder
                    .setStatus(OptionResult.FAILURE)
                    .setMessageCode(UserOptionMessage.NICKNAME_OCCUPIED_MSG.getMessageCode())
                    .build();
        }else {
            return optionResultBuilder
                    .setStatus(OptionResult.SUCCESS)
                    .setMessageCode(UserOptionMessage.NICKNAME_AVAILABLE_MSG.getMessageCode())
                    .build();
        }
    }

    //电话是否被注册
    @GetMapping(value = "/signup",params = {"zrTelephone"})
    public OptionResult isTelephoneOccupied(@RequestParam(name = "zrTelephone") String tel){
        OptionResultBuilder optionResultBuilder = new OptionResultBuilder()
                .setOptionCode(UserOption.USER_SIGNUP.getOptionCode());
        if (zrUserService.isTelRegistered(tel)){
            return optionResultBuilder
                    .setStatus(OptionResult.FAILURE)
                    .setMessageCode(UserOptionMessage.SIGNUP_TELEPHONE_OCCUPIED_MSG.getMessageCode())
                    .build();
        }else {
            return optionResultBuilder
                    .setStatus(OptionResult.SUCCESS)
                    .setOptionCode(UserOptionMessage.SIGNUP_TELEPHONE_AVAILABLE_MSG.getMessageCode())
                    .build();
        }
    }

    //更新
    @PutMapping("/update")
    @ResponseBody
    public OptionResult updateUser(ZRUser zrUser){
        System.out.println(zrUser);
        return zrUserService.updateUser(zrUser);
    }

//    @PutMapping("/")
//    public UserOptionResult updateUserNickname(){
//
//    }

    //删除
    @DeleteMapping("")
    public void deleteUser(@RequestParam(name = "zrId")String zrId){
        System.out.println(zrId);
        zrUserService.deleteUser(zrId);
    }




}
